package com.wenhui.nzapp.ui.activity.dinnerqr;

import static java.math.BigDecimal.ROUND_HALF_UP;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzDinnerQrcode;
import com.gzwh.weblib.bean.NzDinnerRegion;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;
import com.wenhui.nzapp.R;
import com.wenhui.nzapp.ui.adapter.DinnerQrAdapter;
import com.wenhui.nzapp.ui.adapter.DinnerQrAreAdapter;
import com.wenhuitec.productlib.ui.adapter.MenuCategoryAdapter;
import com.wenhuitec.sysui.TitleBuilder;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class DinnerQrIndexActivity extends AppCompatActivity {
    private Context mContext;

    private RecyclerView leftCateRv;

    private RecyclerView rightFoodRv;

    private DinnerQrAreAdapter dinnerQrAreAdapter;

    private int CAMERA_REQ_CODE = 11110;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dinnerqr_index);
        mContext = this;
        new TitleBuilder(this)
                .setTitleText("点餐码设置")
//                .setLeftIco(R.drawable.yp)
                .setLeftIcoListening((view) -> finish());
        findViewById(R.id.qrAreManagerBut).setOnClickListener(view -> {
            startActivity(new Intent(mContext, DinnerQrAreActivity.class));
        });
        findViewById(R.id.qrTableEditBut).setOnClickListener(view -> {
            startActivity(new Intent(mContext, DinnerQrTableEditActivity.class));


        });
        leftCateRv = findViewById(R.id.leftCateRv);
        rightFoodRv = findViewById(R.id.rightFoodRv);
        this.requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQ_CODE);


    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.getAreList(mContext, new MyObserver<List<NzDinnerRegion>>(mContext, false) {
            @Override
            public void onSuccess(Result<List<NzDinnerRegion>> result) {
                System.out.println(result.toString());
                leftCateRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
                dinnerQrAreAdapter = new DinnerQrAreAdapter(mContext, result.getResult());
                leftCateRv.setAdapter(dinnerQrAreAdapter);
                if (result.getResult().size() > 0) {
                    getQr(result.getResult().get(0).getId());
                }

            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }

    private void getQr(String areId) {
        RequestUtils.getQrList(mContext, areId, new MyObserver<List<NzDinnerQrcode>>(mContext, false) {
            @Override
            public void onSuccess(Result<List<NzDinnerQrcode>> result) {
                System.out.println(result.toString());
                rightFoodRv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
                DinnerQrAdapter dinnerQrAdapter = new DinnerQrAdapter(mContext, result.getResult());
                dinnerQrAdapter.setmOnUserClick(dTreeCategoryModel -> {
                    DinnerQrTableEditActivity.startAct(mContext, dTreeCategoryModel);
                });
                rightFoodRv.setAdapter(dinnerQrAdapter);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }


}
